package com.wanxi.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wanxi.VO.DeviceClassVO;
import com.wanxi.dao.DeviceClassDao;
import com.wanxi.dto.ResultDTO;
import com.wanxi.entity.DeviceClassEntity;
import com.wanxi.entity.DeviceEntity;
import com.wanxi.service.DeviceClassService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : 林子淳
 * @create 2023/2/28 18:25
 */
@Service("deviceClassService")
public class DeviceClassServiceImpl implements DeviceClassService {
    @Autowired
    DeviceClassDao deviceClassDao;
    @Autowired
    DeviceClassEntity deviceClassEntity;

    Jedis jedis = new Jedis();
    public ResultDTO findAll(DeviceClassVO deviceClassVO) {
//        DeviceClassEntity deviceClassEntity = new DeviceClassEntity();
        ObjectMapper mapper = new ObjectMapper();
        BeanUtils.copyProperties(deviceClassVO,deviceClassEntity);
        int count = deviceClassDao.count(deviceClassEntity);
        List<DeviceClassEntity> list = new ArrayList<>();
//        list = deviceClassDao.findAll(deviceClassEntity);
        if (deviceClassEntity.getPage() !=null && deviceClassEntity.getPage()==1 && jedis.exists("deviceClassList")){
            String jedisDeviceClassList = jedis.get("deviceClassList");
            JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, DeviceClassEntity.class);
            try {
                list = mapper.readValue(jedisDeviceClassList, javaType);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }else{
            list = deviceClassDao.findAll(deviceClassEntity);
            try {
                String deviceClassList = mapper.writeValueAsString(list);
                jedis.set("deviceClassList",deviceClassList);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }

        return ResultDTO.getResult("", count, list);
    }
    public ResultDTO add(DeviceClassVO deviceClassVO) {
//        DeviceClassEntity deviceClassEntity = new DeviceClassEntity();
        BeanUtils.copyProperties(deviceClassVO,deviceClassEntity);
        int count = deviceClassDao.add(deviceClassEntity);
        jedis.del("deviceClassList");
        return ResultDTO.getResult(count);
    }
    public ResultDTO delete(DeviceClassVO deviceClassVO) {
//        DeviceClassEntity deviceClassEntity = new DeviceClassEntity();
        BeanUtils.copyProperties(deviceClassVO,deviceClassEntity);
        int delete = deviceClassDao.delete(deviceClassEntity);
        jedis.del("deviceClassList");
        return ResultDTO.getResult(delete);
    }
    public ResultDTO findById(DeviceClassVO deviceClassVO) {
//        DeviceClassEntity deviceClassEntity = new DeviceClassEntity();
        BeanUtils.copyProperties(deviceClassVO,deviceClassEntity);
        DeviceClassEntity byId = deviceClassDao.findById(deviceClassEntity);
        return ResultDTO.getResult(byId);
    }
    public ResultDTO update(DeviceClassVO deviceClassVO) {
//        DeviceClassEntity deviceClassEntity = new DeviceClassEntity();
        BeanUtils.copyProperties(deviceClassVO,deviceClassEntity);
        int update = deviceClassDao.update(deviceClassEntity);
        jedis.del("deviceClassList");
        return ResultDTO.getResult(update);
    }
}
